<!-- 账户管理-用户管理-数据列表 -->
<template>
  <!-- 顶部搜索区 -->
  <div class="main-app" v-loading="loading">
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-top: 5px; margin-bottom: 15px;">
      <el-breadcrumb-item :to="{path:'/'}">{{$t('common.nav.home')}}</el-breadcrumb-item>
      <el-breadcrumb-item>{{$t('common.nav.accountManage')}}</el-breadcrumb-item>
      <el-breadcrumb-item>{{$t('common.nav.userManage')}}</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-form :inline="true" :model="searchParam" class="form-inline">
        <el-form-item :label="$t('common.label.name')+':'">
          <el-input v-model="searchParam.name" clearable />
        </el-form-item>
        <el-form-item :label="$t('common.label.phone')+':'">
          <el-input v-model="searchParam.phone" clearable />
        </el-form-item>
        <el-form-item :label="$t('common.label.status')+':'">
          <el-select v-model="searchParam.status" :placeholder="$t('common.placeholder.pleaseSelect')" clearable>
            <el-option :label="$t('common.label.enable')" value="0" />
            <el-option :label="$t('common.label.disable')" value="1" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">{{$t('common.button.search')}}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-refresh-right" @click="reset">{{$t('common.button.reset')}}</el-button>
        </el-form-item>
      </el-form>
      <el-form :inline="true" class="toolBoxTop" style="height:37px;">
        <el-button type="primary" icon="el-icon-plus" @click="showViews($t('common.button.add'))">{{$t('common.button.add')}}</el-button>
      </el-form>
    </el-card>
    <!-- 数据列表区 -->
    <div class="show-result">
      <div class="table">
        <el-table id="table" ref="multipleTable" tooltip-effect="dark" stripe style="width: 100%" :data="tableData" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column prop="name" :label="$t('common.label.name')" align="center"/>
          <el-table-column prop="sex" :label="$t('common.label.sex')" width="100" align="center">
            <template slot-scope="scope">
              <el-tag style="width: 100%; text-align: center" effect="dark" :class="'bg-' + scope.row.sexColor">{{ scope.row.sex }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="roleName" :label="$t('common.label.roleName')" align="center"/>
          <el-table-column prop="jobNumber" :label="$t('common.label.jobNumber')" align="center"/>
          <el-table-column prop="accountNumber" :label="$t('common.label.accountNumber')" align="center"/>
          <el-table-column prop="phone" :label="$t('common.label.phone')" align="center"/>
          <el-table-column prop="email" :label="$t('common.label.email')" align="center"/>
          <el-table-column prop="status" :label="$t('common.label.status')" width="100" align="center">
            <template slot-scope="scope">
              <el-switch class="switch"
                v-model="scope.row.switchenable"
                @change="switchChange(scope.row)"
                :active-text="$t('common.label.enable')"
                active-color="#369BA5"
                :inactive-text="$t('common.label.disable')"
                inactive-color="#EC808D"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="remark" :label="$t('common.label.remark')" align="center"/>
          <el-table-column prop="createTime" :label="$t('common.label.createTime')" width="180" align="center">
            <template slot-scope="scope">
              {{ scope.row.createTime | parseDate }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" :label="$t('common.label.operate')" align="center" width="200">
            <template slot-scope="scope">
              <el-button-group>
                <el-button size="mini" type="primary" @click="operation($t('common.button.edit'), scope.row)">{{$t('common.button.edit')}}</el-button>
                <el-button size="mini" type="danger" @click="operation($t('common.button.del'), scope.row)">{{$t('common.button.del')}}</el-button>
              </el-button-group>
            </template>
            <!-- <template slot-scope="scope">
              <el-button-group>
                <el-button v-for="item in tionsbtn" :key="item.id" :type="item.meta.icon" size="mini" @click="operation(item.name, scope.row)">
                  {{ item.meta.title }}
                </el-button>
              </el-button-group>
            </template> -->
          </el-table-column>
        </el-table>
      </div>
      <!-- 列表底部分页按钮 -->
      <div class="page">
        <el-pagination background layout="total, prev, pager, next, sizes"
          :total="total"
          :current-page.sync="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { pageMixin } from "../../../mixins/index";
import { getPhone } from "../../../utils/index.js";
import {
  UserList,
  UpdateUserStatus,
  DelUser,
} from "../../../api/user.js";
import Qs from "qs";
export default {
  // 页面公共变量
  data() {
    return {
      searchParam: {},          // 搜索条件参数
      dialogVisible: false,     // 是否隐藏提示框
    };
  },
  // 页面创建完成事件
  async created() {
    await this.init();
  },
  // 数据计算事件
  computed: {
    deleteDisabled() {
      return this.ids.length === 0;
    },
  },
  // 数据过滤器
  filters: {
    // 转换日期显示格式
    parseDate(value) {
      if (value) {
        return value.replace("T", " ");
      }
    },
  },
  // 表格分页属性配置
  mixins: [pageMixin],
  // 页面相关方法
  methods: {
    // 初始化加载事件
    async init() {
      this.tableData = [];        // 重置表格数据源
      this.loading = true;        // 启动加载框

      // 获取数据
      const { data, count } = await UserList({
        page: this.currentPage,
        limit: this.pageSize,
        Name: this.searchParam.name,
        Phone: this.searchParam.phone,
        Status: this.searchParam.status
      });
      this.tableData = data;      // 设置表格数据源

      // 转换表格数据源部分字段显示
      this.tableData.forEach((item) => {
        item.phone = getPhone(item.phone)
        if (item.sex === "1") {
          item.sex = this.$t('common.label.woman');
          item.sexColor = "red";
        } else {
          item.sex = this.$t('common.label.man');
          item.sexColor = "green";
        }
        if (item.status.toString() === "0") {
          item.status = this.$t('common.label.enable');
          item.switchenable = true;
        } else {
          item.status = this.$t('common.label.disable');
          item.switchenable = false;
        }
      });

      this.total = count;         // 设置表格总数据量
      this.loading = false;       // 关闭加载框
    },
    // 表格重置事件
    async reset() {
      this.searchParam = {
        name: "",
        phone: "",
        status: ""
      };
      await this.init();
    },
    // 列表顶部功能按钮点击事件
    showViews(action) {
      switch (action) {
        // 新建
        case this.$t('common.button.add'):
          this.$router.push("user/create");
          break;
      }
    },
    // 操作功能按钮点击事件
    operation(value, row) {
      switch (value) {
        // 编辑
        case this.$t('common.button.edit'):
          this.$router.push(`user/edit/${row.id}`);
          break;
        // 删除
        case this.$t('common.button.del'):
          this.delData(row.id);
          break;
      }
    },
    // 表格switch组件切换事件
    switchChange(row) {
      if (row.switchenable === false) {
        this.enable("1", row.id);
      } else {
        this.enable("0", row.id);
      }
    },
    // 启用或禁用事件
    async enable(value, id) {
      let confimText = "";
      if (value === "0") {
        confimText = this.$t('common.label.enable');
      } else {
        confimText = this.$t('common.label.disable');
      }
      this.$confirm(this.$t('common.prompt.info.isUpdateStatus'), this.$t('common.dialog.tip'), {
        confirmButtonText: this.$t('common.dialog.confirm'),
        cancelButtonText: this.$t('common.dialog.cancel'),
        type: "warning",
      }).then(async () => {
        // 操作数据
        const {success} = await UpdateUserStatus({
          Id: id,
          Status: value
        });
        // 刷新页面
        await this.init();
        // 根据返回值弹出信息
        if(success) {
          this.$message({
            type: "success",
            message: `${confimText}${this.$t('common.prompt.success.updateStatus')}`,
          });
        } else {
          this.$message({
            type: "error",
            message: `${confimText}${this.$t('common.prompt.error.updateStatus')}`,
          });
        }
      });
    },
    // 根据主键ID删除数据
    async delData(id) {
      this.$confirm(this.$t('common.prompt.info.isDelSelData'), this.$t('common.dialog.tip'), {
        confirmButtonText: this.$t('common.dialog.confirm'),
        cancelButtonText: this.$t('common.dialog.cancel'),
      }).then(async() => {
        const data = await DelUser({Id: id});
        this.init();
        if (data) {
          this.$alert(this.$t('common.prompt.success.del'), this.$t('common.dialog.tip'), {
            confirmButtonText: this.$t('common.dialog.confirm'),
            type: "success",
          });
        } else {
          this.$alert(this.$t('common.prompt.error.del'), this.$t('common.dialog.tip'), {
            confirmButtonText: this.$t('common.dialog.confirm'),
            type: "danger",
          });
        }
      });
    }
  },
};
</script>

<style lang="scss" scoped>
// 顶部按钮绝对定位
.formbtn { text-align: right; }
// 数据表格区样式
.show-result { margin-top: 1%; }
/* 操作项开关样式 */
::v-deep .switch .el-switch__label { position: absolute; display: none; color: #fff; }
/* 打开时文字位置设置 */
::v-deep .switch .el-switch__label--right { z-index: 1; right: 5px; }
/* 关闭时文字位置设置 */
::v-deep .switch .el-switch__label--left { z-index: 1; left: 8px; }
/* 显示文字 */
::v-deep .switch .el-switch__label.is-active { display: block; }
::v-deep .switch.el-switch .el-switch__core,
::v-deep .el-switch .el-switch__label { width: 60px !important; }
/* 表格隔行换色样式 */
::v-deep .el-table--striped .el-table__body tr.el-table__row--striped td { background-color: #F2F2F2; }
/* 表格数据行高 */
::v-deep .el-table td { padding: 8px 0px; }
/* 表头背景色样式 */
::v-deep .el-table th{ background-color: #DBEDEF !important; }
/* 表头数据行内线样式 */
::v-deep .el-table td,.building-top .el-table th.is-leaf { border-bottom: 0px; }
/* 自定义提示框样式 */
.dialog_diy {
  ::v-deep .el-dialog__wrapper {
    .el-dialog {
      .el-dialog__body { padding: 0px; }
      .el-dialog__header { padding: 0px; }
      .el-dialog__headerbtn { top: 5px; right: 5px; padding-top: 10px; }
      .el-dialog__headerbtn .el-dialog__close { color: #fff; height: 30px; width: 35px; }
      .el-dialog__headerbtn .el-dialog__close:hover { color: gray; }
    }
  }
}
</style>
